package com.daikit.daikit4gxt.client.ui.editor.field;

import com.daikit.commons.shared.utils.DkStringUtils;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.ui.component.DkSpacer;
import com.daikit.daikit4gxt.client.ui.editor.advanced.AbstractDkHideableAdapterField;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.resources.client.ImageResource;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.cell.CellComponent;
import com.sencha.gxt.widget.core.client.container.HBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;


/**
 * Field with a button
 *
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <T>
 */
public abstract class DkFieldWrapperWithButton<T> extends AbstractDkHideableAdapterField<T>
{
	private final CellComponent<T> cellComponent;
	private final TextButton button;
	private boolean enabled = true;
	private int buttonWidth;

	private int height = 26;

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// CONSTRUCTORS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Wrapper to {@link #DkFieldWrapperWithButton(CellComponent, String)} without tooltip on add button
	 *
	 * @param cellComponent
	 *           a {@link CellComponent}
	 */
	public DkFieldWrapperWithButton(final CellComponent<T> cellComponent)
	{
		this(cellComponent, null);
	}

	/**
	 * Constructor
	 *
	 * @param cellComponent
	 *           a {@link CellComponent}
	 * @param buttonTooltip
	 *           the button tooltip (leave it to null if you don't want tooltip)
	 */
	public DkFieldWrapperWithButton(final CellComponent<T> cellComponent, final String buttonTooltip)
	{
		super(new HBoxLayoutContainer());

		this.cellComponent = cellComponent;

		final HBoxLayoutContainer line = (HBoxLayoutContainer) getWidget();
		line.getElement().getStyle().setMarginBottom(2, Unit.PX);

		button = createButton(buttonTooltip);
		line.add(cellComponent);
		line.add(new DkSpacer(SPACERWIDTH));
		line.add(button);
		setHeight(height);

		line.addResizeHandler(new ResizeHandler()
		{
			@Override
			public void onResize(final ResizeEvent event)
			{
				final int width = event.getWidth();
				if (width > 0)
				{
					final int fieldWidth = width - buttonWidth - SPACERWIDTH;
					cellComponent.setWidth(fieldWidth);
				}
			}
		});
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// METHODS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * Create the button
	 *
	 * @param buttonTooltip
	 *           the tooltip
	 * @return the created button
	 */
	protected TextButton createButton(final String buttonTooltip)
	{
		final TextButton button = new TextButton()
		{
			@Override
			public void setWidth(final int width)
			{
				super.setWidth(width);
				buttonWidth = width;
			}
		};
		button.addSelectHandler(new SelectHandler()
		{
			@Override
			public void onSelect(final SelectEvent event)
			{
				onButtonClicked();
			}
		});
		button.setIcon(DkBaseMain.icons().add2_16());
		button.setWidth(30);
		if (DkStringUtils.hasText(buttonTooltip))
		{
			button.setToolTip(buttonTooltip);
		}
		return button;
	}

	protected abstract void onButtonClicked();

	/*
	 * (non-Javadoc)
	 *
	 * @see com.sencha.gxt.widget.core.client.Component#setEnabled(boolean)
	 */
	@Override
	public void setEnabled(final boolean enabled)
	{
		cellComponent.setEnabled(enabled);
		button.setEnabled(enabled);
		this.enabled = enabled;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.sencha.gxt.widget.core.client.Component#setWidth(int)
	 */
	@Override
	public void setWidth(final int width)
	{
		final int fieldWidth = width - buttonWidth - SPACERWIDTH;
		cellComponent.setWidth(fieldWidth);
		super.setWidth(width);
	}

	@Override
	public void setValue(final T value)
	{
		cellComponent.setValue(value);
	}

	@Override
	public T getValue()
	{
		return cellComponent.getValue();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.sencha.gxt.widget.core.client.Component#isEnabled()
	 */
	@Override
	public boolean isEnabled()
	{
		return enabled;
	}

	/**
	 * @param height
	 *           the height to set
	 */
	@Override
	public void setHeight(final int height)
	{
		this.height = height;
		cellComponent.setHeight(height);
		((HBoxLayoutContainer) getWidget()).setHeight(height);
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @return the lineHeight
	 */
	public int getHeight()
	{
		return height;
	}

	/**
	 * @return the cellComponent
	 */
	public CellComponent<T> getCellComponent()
	{
		return cellComponent;
	}

	/**
	 * Wrapper to {@link #getCellComponent()}
	 *
	 * @return the cellComponent
	 */
	public CellComponent<T> getField()
	{
		return getCellComponent();
	}

	/**
	 * Set the button tooltip
	 *
	 * @param buttonTooltip
	 */
	public void setButtonTooltip(final String buttonTooltip)
	{
		this.button.setToolTip(buttonTooltip);
	}

	/**
	 * Set the button icon
	 *
	 * @param icon
	 *           an {@link ImageResource}
	 */
	public void setButtonIcon(final ImageResource icon)
	{
		button.setIcon(icon);
	}

	/**
	 * Get the button
	 *
	 * @return the {@link TextButton}
	 */
	public TextButton getButton()
	{
		return button;
	}
}
